package com.fg.cloud.factory.controller.system;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.fg.cloud.common.ArrayUtils;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.construction.Select;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.shiro.UserUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.agency.vo.AgencyUserVo;
import com.fg.cloud.manager.entry.system.InvoiceManageEntity;
import com.fg.cloud.manager.service.system.InvoiceManageService;

/**
 * Description:发票管理
 * @author 陈银龙
 * @date 2017年9月15日上午9:49:40
 */
@Controller
@RequestMapping("invoiceManage")
public class InvoiceManageContorller extends BaseController{

	@Autowired
	private InvoiceManageService invoiceManageService;
	
	/**
	 * Description:发票管理查询,无则添加
	 * @param req
	 * @param resp
	 * @author 陈银龙
	 * @date 2017年9月15日上午10:21:06
	 */
/*	@RequestMapping(value="/selectPower",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectPower(HttpServletRequest req , HttpServletResponse resp){
		try {
			Map<String, Object> usmap = new HashMap<String, Object>();
			usmap.put("facId", getCurrentFactoryUser().getFacId());
			InvoiceControlEntity ice = invoiceManageService.selectPower(usmap);
			if(ice == null ){// 无则添加
				invoiceManageService.insertPower(usmap);
				ice = invoiceManageService.selectPower(usmap);
				doResult(resp, true, StatusCode.FINDSUCCESS, ice, ResultType.FIND_SUCCESS);
			}else{
				doResult(resp, true, StatusCode.FINDSUCCESS, ice, ResultType.FIND_SUCCESS);
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.FINDSUCCESS, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}*/
	
	/**
	 * Description:厂商修改发票权限
	 * @param req
	 * @param resp
	 * @param id
	 * @author 陈银龙
	 * @date 2017年9月15日上午11:30:21
	 */
	/*@RequestMapping(value="/updatePower",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void updatePower(HttpServletRequest req , HttpServletResponse resp, InvoiceControlEntity ice){
		try {
			if(ice.getId() == null || ice.getId() < 0){
				doResult(resp, false, StatusCode.LACKVALUE, null, "id不能为空");
				return;
			}
			ice.setFacId(getCurrentFactoryUser().getFacId());
			Long flag = invoiceManageService.updatePower(ice);
			if(flag > 0 ){
				doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.SAVE_SUCCESS);
			}else{
				doResult(resp, false, StatusCode.SAVEERROR, flag, ResultType.SAVE_FAULT);
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.SAVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}*/
	
	/**
	 * Description:发票新增
	 * @param req
	 * @param resp
	 * @param invoice
	 * @author 陈银龙
	 * @date 2017年9月15日上午10:21:06
	 */
	@RequestMapping(value="/insert",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void insert(HttpServletRequest req , HttpServletResponse resp , InvoiceManageEntity invoice){
		try {
			invoice.setFacId(getCurrentFactoryUser().getFacId());
			invoice.setCreator(getCurrentFactoryUser().getId());
			invoice.setUpdater(getCurrentFactoryUser().getId());
			if (invoice == null || StringUtils.isEmpty(invoice.getInvoiceName()) || invoice.getTaxRate() == null || invoice.getTaxRate().compareTo(new BigDecimal(0)) == -1){
				doResult(resp, false, StatusCode.LACKVALUE, null, "发票名和税率错误");
				return;
			}else{
				Long flag = invoiceManageService.insert(invoice);
				if(flag > 0 ){
					doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.ADD_SUCCESS);
				}else{
					doResult(resp, false, StatusCode.SAVEERROR, flag, ResultType.ADD_FAULT);
				}
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.SAVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * Description:发票修改
	 * @param req
	 * @param resp
	 * @param invoice
	 * @author 陈银龙
	 * @date 2017年9月15日上午10:31:03
	 */
	@RequestMapping(value="/update",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void update(HttpServletRequest req , HttpServletResponse resp , InvoiceManageEntity invoice){
		try {
			invoice.setFacId(getCurrentFactoryUser().getFacId());
			invoice.setUpdater(getCurrentFactoryUser().getId());
			if (invoice == null ||  invoice.getId() == null || invoice.getId() < 0
					|| StringUtils.isEmpty(invoice.getInvoiceName()) || invoice.getTaxRate() == null || invoice.getTaxRate().compareTo(new BigDecimal(0)) == -1){
				doResult(resp, false, StatusCode.LACKVALUE, null, "参数错误");
				return;	
			}else{
				Long flag = invoiceManageService.update(invoice);
				if(flag > 0 ){
					doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.SAVE_SUCCESS);
				}else{
					doResult(resp, false, StatusCode.SAVEERROR, flag, ResultType.SAVE_FAULT);
				}
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.SAVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:发票删除
	 * @param req
	 * @param resp
	 * @param ids
	 * @author 陈银龙
	 * @date 2017年9月15日上午10:54:25
	 */
	@RequestMapping(value="/delete",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void delete(HttpServletRequest req , HttpServletResponse resp , String ids){
		try {
			if (ids == null ||  ids.length() == 0){
				doResult(resp, false, StatusCode.LACKVALUE, null, "id不能为空");
				return;	
			}else{
				String[] id = ids.split(",");
				Long[] idarray = ArrayUtils.convert(id);
				Map<String, Object> usmap = new HashMap<String, Object>();
				usmap.put("ids", idarray);
				usmap.put("facId", getCurrentFactoryUser().getFacId());
				Long flag = invoiceManageService.delete(usmap);
				if(flag > 0 ){
					doResult(resp, true, StatusCode.REMOVESUCCESS, flag, ResultType.REMOVE_SUCCESS);
				}else{
					doResult(resp, false, StatusCode.REMOVEERROR, flag, ResultType.REMOVE_FAULT);
				}
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.REMOVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询发票,列表分页模式
	 * */
	@RequestMapping(value="/select",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void select(HttpServletRequest req , HttpServletResponse resp, String sort,Integer page,Integer rows,String order){
		try {
			if(page == null || page < 0 || rows ==null || rows < 0){
				doResult(resp, false, StatusCode.LACKVALUE, null, "page,rows不能为空");
				return;	
			}
			PageUtils<InvoiceManageEntity> pageinfo = new PageUtils<InvoiceManageEntity>(page, rows, sort, order);
			Map<String, Object> usmap = pageinfo.getCondition();
			usmap.put("facId", getCurrentFactoryUser().getFacId());
			invoiceManageService.select(pageinfo);
			doResult(resp, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(resp, true, StatusCode.FINDSUCCESS, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询发票,经销商select模式
	 * */
	@RequestMapping(value="/selectForAgency",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectForAgency(HttpServletRequest req , HttpServletResponse resp){
		try {
			AgencyUserVo user =(AgencyUserVo)UserUtils.getCurrentUser();
			Map<String, Object> usmap = new HashMap<String, Object>();
			usmap.put("facId", user.getFacId());
			List<Select> list = invoiceManageService.selectAllInvoice(usmap);
			doResult(resp, true, StatusCode.FINDSUCCESS, list, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(resp, true, StatusCode.FINDSUCCESS, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询发票,厂商select模式
	 * */
	@RequestMapping(value="/selectForFactory",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectForFactory(HttpServletRequest req , HttpServletResponse resp){
		try {
			Map<String, Object> usmap = new HashMap<String, Object>();
			usmap.put("facId", getCurrentFactoryUser().getFacId());
			List<Select> list = invoiceManageService.selectAllInvoice(usmap);
			doResult(resp, true, StatusCode.FINDSUCCESS, list, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(resp, true, StatusCode.FINDSUCCESS, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
}
